package cn.edu.swu.xgb.utils;

import cn.edu.swu.xgb.book.Book;
import java.util.List;

public class HtmlHelper {

    public static String wrapHtml(String content) {
        String template = """
<html>
    <head>
      <meta charset="utf-8">
      <title>西大网上书城</title>
      <link rel="stylesheet" href="../css/bookstore.css" />
    </head>
<body>
    <div class="main-content">
        <div class="page-header">
            <h1>欢迎访问西大网上书城</h1>
        </div>
        
        <div class="action-bar">
            <div class="action-links">
                <a href="./add_book.html" class="btn btn-primary">添加图书</a>
                <a href="./books" class="btn btn-primary">显示图书</a>
                <a href="./logout" class="btn btn-error">退出系统</a>
            </div>
        </div>
        
        <br>
        <div class="search-form">
            <form action="./searchBook" method="get">
                <input type="text" name="content" class="form-control" placeholder="输入书名或作者搜索...">
                <button type="submit" class="btn btn-primary">查询</button>
            </form>
        </div>
        
        %s
    </div>
</body>
</html>
        """;
        return String.format(template, content);
    }


    /**
     * 该函数将传入的图书列表对象转换成 html 的表格
     * @param books
     * @return
     */
    public static String buildBooksTable(List<Book> books) {
        if (books.isEmpty()) {
            return "<div class='empty-state'>暂无图书数据</div>";
        }

        StringBuilder sb = new StringBuilder();
        sb.append("<div class='table-container fade-in'>");
        sb.append("<table class='tb-book'>");
        sb.append("<thead><tr>")
                .append("<th>编号</th><th>书名</th><th>作者</th><th>价格</th>")
                .append("<th>备注</th><th>出版日期</th><th>封面图片</th><th colspan='2'>操作</th>")
                .append("</tr></thead>");
        sb.append("<tbody>");

        String template = """
        <tr class='book-row'>
            <td class='text-center book-id'>%s</td>
            <td class='book-name'><strong>%s</strong></td>
            <td class='text-center book-author'>%s</td>
            <td class='text-center book-price'><span class='price'>¥%.2f</span></td>
            <td class='book-memo'>%s</td>
            <td class='text-center book-publish'>%s</td>
            <td class='text-center book-image'>%s</td>
            <td class='text-center action-cell'>
                <a href='./deleteBook?id=%s' class='btn btn-error btn-sm' onclick='return confirm(\"确定要删除这本书吗？\")'>删除</a>
            </td>
            <td class='text-center action-cell'>
                <a href='./updateBook?id=%s' class='btn btn-warning btn-sm'>修改</a>
            </td>
        </tr>
    """;

        for (Book b : books) {
            String imageHtml = (b.getPicture() == null || b.getPicture().isEmpty())
                    ? "<div class='no-image'>无图片</div>"
                    : String.format("<img src='../upload/%s' width='80' class='book-cover'/>", b.getPicture());

            String memo = (b.getMemo() == null || b.getMemo().isEmpty()) ? "无备注" : b.getMemo();

            sb.append(String.format(template,
                    b.getId(),
                    b.getName(),
                    b.getAuthor(),
                    b.getPrice(),
                    memo,
                    b.getPublish(),
                    imageHtml,
                    b.getId(),
                    b.getId()
            ));
        }

        sb.append("</tbody>");
        sb.append("</table>");

        sb.append(String.format("<div class='table-footer'>共 <span class='book-count'>%d</span> 本图书</div>", books.size()));
        sb.append("</div>");

        return sb.toString();
    }
}
